﻿#!/usr/bin/python
# -*- encoding: utf-8 -*-
'''
@File    : UploadModelWidget.py
@Desc    : 
@Version : 1.0
@Author  : echohelper 
@Email   : echohelper@qq.com
@WebSite : www.github.com/echohelper
@Time    : 2021/09/24 09:19:47
'''
# Start typing your code from here

from PyQt5 import QtCore, QtGui, QtWidgets

from module.project.upload_model.Ui_UploadModelWidget import Ui_Form
from module.network.http.LoginGraphicEngine  import LoginGraphicEngine

import os
import configparser


class UploadModelWidget(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.initUi()
        self.LoginAddr = ''
        self.Addr = ''
        self.Key = ''
        self.Pwd = ''
        self.token = ''

    def initUi(self) -> None:
        inier = configparser.ConfigParser()
        # 当前文件路径
        curDir = os.path.split(os.path.realpath(__file__))[0]
        # 在当前文件路径下查找.ini文件
        filePath = os.path.join(curDir, "config_engine3.0.ini")
        inier.read(filePath)
        configer = inier['Config']
        self.LoginAddr = configer['LoginAddr']
        self.Addr = configer['Addr']
        self.Key = configer['Key']
        self.Pwd = configer['Pwd']

        self.ui.lineEditLoginAddr.setText(self.LoginAddr)
        self.ui.lineEditAddr.setText(self.Addr)
        self.ui.lineEditKey.setText(self.Key)
        self.ui.lineEditPwd.setText(self.Pwd)

        LoginGraphicEngine().initAddr(self.Addr, self.LoginAddr)

    def onBtnClear(self) -> None:
        self.ui.textBrowser.clear()

    def onBtnSave(self) -> None:
        filePath, _ = QtWidgets.QFileDialog.getSaveFileName(None, 
            "Save File", "log.txt", "Text Files (*.txt)")
        if filePath:
            self.ui.textBrowser.append("Save Path:" + filePath)

    def onBtnLogin(self) -> None:
        addr = self.ui.lineEditLoginAddr.text()
        data = '{"clientId":"yizhu","password":"' + self.ui.lineEditPwd.text()+ '","username":"' + self.ui.lineEditKey.text() + '"}'
        bRet = LoginGraphicEngine().login(self.ui.lineEditLoginAddr.text(), data)
        if bRet:
            self.ui.textBrowser.append('Account Login Successful')
        else:
            self.ui.textBrowser.append('Account Login Failure')

    def onBtnModelList(self) -> None:
        pass

    def onBtnSelect(self) -> None:
        filePaths, _ = QtWidgets.QFileDialog.getOpenFileNames(None,
            "Select Model Files", "F:/desktop/129模型", "Model Files (*.*)")
        
        self.ui.listWidget.addItems(filePaths)

        self.ui.textBrowser.append("Select Path:")
        for filePath in filePaths:
            self.ui.textBrowser.append(filePath)

    def onBtnUpload(self) -> None:
        item = self.ui.listWidget.currentItem()
        if item == None:
            self.ui.textBrowser.append('请选中要上传的文件')
            return

        filePath = item.text()
        LoginGraphicEngine().uploadModelFile(filePath)

    def onBtnDownload(self) -> None:
        filePath = QtWidgets.QFileDialog.getExistingDirectory(None, "Select Folder", "/")
        if filePath:
            self.ui.textBrowser.append("Save Path:" + filePath)
        bRet = LoginGraphicEngine().downloadModelFile(filePath + '/')
        if(bRet):
            self.ui.textBrowser.append('下载成功')
        else:
            self.ui.textBrowser.append('下载失败')
